/**
 * @Created by   on 2019-6-18 14:56:52.
 * @updated by
 * @description 额度信息
 */
define(['./libs/js-xlsx/xlsx.full.min.js'], function (require, exports) {
  /**
   * 页面加载完成时触发
   * @param hashCode 路由ID
   * @param data 传递数据对象
   * @param cite 页面站点信息
   */
  exports.ready = function (hashCode, data, cite) {
    yufp.custom.vue({
      el: cite.el,
      data: function () {
        return {
          dataUrl: backend.msScriptsLimitService + '/api/limitinfo/query',
          formdata: {},
          dialogVisible: false,
          formDisabled: false,
          viewType: 'DETAIL',
          viewTitle: yufp.lookup.find('CRUD_TYPE', false),
          saveBtnShow: true,
          certType: [
            { 'key': '10110', 'value': '身份证' }
          ],
          addLimitStatus: [
            { 'key': '00', 'value': '待审批 ' },
            { 'key': '01', 'value': '审批中' },
            { 'key': '02', 'value': '正常（审批通过）' },
            { 'key': '88', 'value': '冻结' },
            { 'key': '99', 'value': '失效' }
          ],
          otherLimitStatus: [
            { 'key': '00', 'value': '待审批 ' },
            { 'key': '01', 'value': '审批中' },
            { 'key': '02', 'value': '正常（审批通过）' },
            { 'key': '88', 'value': '冻结' },
            { 'key': '99', 'value': '失效' }
          ],
          limitStatus: this.otherLimitStatus,
          limitType: [
            { 'key': '01', 'value': '非循环' },
            { 'key': '02', 'value': '循环' }
          ],
          itemDisabled: true,
          itemHidden: true,
          cusef: false
        };
      },
      methods: {
        /**
         * 取消
         */
        cancelFn: function () {
          var _this = this;
          _this.dialogVisible = false;
        },
        /**
         * 保存
         */
        saveFn: function () {
          var validate = false;
          var _this = this;
          _this.$refs.refForm.validate(function (valid) {
            validate = valid;
          });
          if (!validate) {
            return;
          }

          var model = {};
          yufp.clone(_this.formdata, model);
          var url = '';
          if (_this.viewType == 'ADD') {
            url = '/api/limitinfo/';
          } else {
            url = '/api/limitinfo/update';
          }
          // 向后台发送保存请求
          yufp.service.request({
            method: 'POST',
            url: backend.msScriptsLimitService + url,
            data: model,
            callback: function (code, message, response) {
              _this.$refs.refTable.remoteData();
              _this.$message('操作成功');
              _this.dialogVisible = false;
            }
          });
        },
        /**
         * 控制保存按钮、xdialog、表单的状态
        * @param viewType 表单类型
        * @param editable 可编辑,默认false
        */
        switchStatus: function (viewType, editable) {
          var _this = this;
          _this.viewType = viewType;
          _this.saveBtnShow = editable;
          _this.dialogVisible = true;
          _this.formDisabled = !editable;
          if (viewType != 'DETAIL') {
            _this.itemDisabled = true;
            _this.itemHidden = true;
          } else {
            _this.itemDisabled = false;
            _this.itemHidden = false;
          }

          if (viewType == 'ADD') {
            _this.cusef = false;
            _this.limitStatus = _this.addLimitStatus;
          } else {
            _this.cusef = true;
            _this.limitStatus = _this.otherLimitStatus;
          }
        },
        /**
         * 新增按钮
         */
        addFn: function () {
          var _this = this;
          _this.switchStatus('ADD', true);
          _this.$nextTick(function () {
            _this.$refs.refForm.resetFields();
          });
        },
        /**
         * 修改
         */
        modifyFn: function () {
          var _this = this;
          if (_this.$refs.refTable.selections.length != 1) {
            _this.$message({ message: '请先选择一条记录', type: 'warning' });
            return;
          }
          if (_this.$refs.refTable.selections[0].limitStatus != '00') {
            _this.$message({ message: '当前额度不为待审批状态，不可修改', type: 'warning' });
            return;
          }
          _this.switchStatus('EDIT', true);
          _this.$nextTick(function () {
            _this.$refs.refForm.resetFields();
            var obj = _this.$refs.refTable.selections[0];
            yufp.clone(obj, _this.formdata);
          });
        },
        /**
         * 详情
         */
        infoFn: function () {
          var _this = this;
          var selectionsAry = _this.$refs.refTable.selections;
          if (selectionsAry.length != 1) {
            _this.$message({ message: '请先选择一条记录', type: 'warning' });
            return;
          }
          _this.switchStatus('DETAIL', false);
          _this.$nextTick(function () {
            _this.$refs.refForm.resetFields();
            yufp.clone(selectionsAry[0], this.formdata);
          });
        },
        /**
         * 删除
         */
        deleteFn: function () {
          var _this = this;
          var selections = _this.$refs.refTable.selections;
          if (selections.length < 1) {
            _this.$message({ message: '请先选择一条记录', type: 'warning' });
            return;
          }
          if (_this.$refs.refTable.selections[0].limitStatus != '00') {
            _this.$message({ message: '当前额度不为待审批状态，不可修改', type: 'warning' });
            return;
          }
          var url = backend.msScriptsLimitService + '/api/limitinfo/delete/' + selections[0].limitNo;
          _this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
            type: 'warning',
            callback: function (action) {
              if (action === 'confirm') {
                yufp.service.request({
                  method: 'GET',
                  url: url,
                  data: '',
                  callback: function (code, message, response) {
                    _this.$refs.refTable.remoteData();
                    _this.$message('操作成功');
                  }
                });
              }
            }
          });
        },
        formatter: function (value, scale) {
          var money = parseFloat((value + '').replace(/[^\d\.-]/g, '')).toFixed(scale) + '';
          var l = money.split('.')[0].split('').reverse();
          var r = money.split('.')[1];
          var t = '';
          for (var i = 0; i < l.length; i++) {
            t += l[i] + ((i + 1) % 3 == 0 && i + 1 != l.length ? ',' : '');
          }
          return t.split('').reverse().join('') + '.' + r;
        },

        formatterPreCreditLimit: function (value) {
          return this.formatter(value.preCreditLimit, 2);
        },

        formatterActualLimit: function (value) {
          return this.formatter(value.actualLimit, 2);
        },

        formatterUsedLimit: function (value) {
          return this.formatter(value.usedLimit, 2);
        },

        formatterActualUsedLimit: function (value) {
          return this.formatter(value.actualUsedLimit, 2);
        },

        addLimitStatusFilter: function (value) {
          if (this.viewType == 'ADD') {
            return this.limitStatus;
          } else {
            var addLimitStatus = [];
            addLimitStatus.push({'key': '00', 'value': '待审批 '});
            return addLimitStatus;
          }
        }
      }
    });
  };
});